﻿using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using PlanningGIS.Util.Configurations;

namespace PlanningGIS.Util.IO
{
    /// <summary>
    /// 文件路径操作类
    /// </summary>
    public class PathHelper
    {
        /// <summary>
        /// 构造函数
        /// </summary>
        public PathHelper()
        {

        }

        /// <summary>
        /// 获取该Dll所在的目录  
        /// </summary>
        /// <returns></returns>
        public static string GetDllPath()
        {
            return System.IO.Path.GetDirectoryName((new PathHelper()).GetType().Assembly.Location);
        }

        /// <summary>
        /// 获取应用程序所在的路径，若是web调用，在返回bin的上一级目录
        /// </summary>
        /// <returns></returns>
        public static string GetAppPath()
        {
            string p = GetDllPath();
            DirectoryInfo di = new DirectoryInfo(p);
            string dname = di.Name;
            if ("bin".Equals(dname, StringComparison.CurrentCultureIgnoreCase))
                return di.Parent.FullName;
            return p;
        }
        /// <summary>
        /// 获取AppSetting指定键值配置所在的路径：Dll + AppSettings[key]
        /// </summary>
        /// <param name="key">键值</param>
        /// <returns></returns>
        public static string GetAppSettingPath(string key)
        {
            string p = GetAppPath();
            string kname = ConfigurationManager.AppSettings.Get(key);
            if (string.IsNullOrEmpty(kname))
                return p;
            return  p + "\\" + kname.Trim();
        }

        /// <summary>
        /// 获取AppSetting指定键值配置所在的路径：Dll + AppSettings[key]
        /// </summary>
        /// <param name="key">键值</param>
        /// <param name="configpath"></param>
        /// <returns></returns>
        public static string GetAppSettingPath(string key,string configpath)
        {
            string p = GetAppPath();
            string kname = ConfigurationHelper.GetAppSettingValue(configpath, key);
            if (string.IsNullOrEmpty(kname))
                return p;
            return p + "\\" + kname.Trim();
        }

        /// <summary>
        /// 获取文件列表
        /// </summary>
        /// <param name="path">目录名称</param>
        /// <param name="searchPattern">条件，多个以【|】间隔</param>
        /// <param name="searchOption">搜索条件</param>
        /// <returns></returns>
        public static string[] GetFiles(string path, string searchPattern = "*.*", SearchOption searchOption = SearchOption.AllDirectories)
        {
            if (string.IsNullOrWhiteSpace(searchPattern)) searchPattern = "*.*";
            string[] searchPatterns = searchPattern.Split('|');
            List<string> files = new List<string>();
            foreach (string sp in searchPatterns)
            {
                if (string.IsNullOrWhiteSpace(sp)) continue;
                string[] fs = System.IO.Directory.GetFiles(path, sp, searchOption);
                if (fs == null || fs.Length == 0) continue;
                files.AddRange(fs);
            }                
            files.Sort();
            return files.ToArray();
        }


    }
}
